import { observable, action } from 'mobx';
import { Bubble, Tools } from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';
import Api, { getMailFolder } from '../../apis/setting';

const { getLabel } = Tools;
const initState = {
  folderName: '',
  template: { default: [{ key: 'col', configs: [{ key: 'col_row', configs: [{ key: 'name' }] }] }] },
  listParams: {
    url: getMailFolder,
    params: {},
  },
  folderCounts: {
    unreadsize: 0,
    totalSize: 0,
    occupyspace: 0,
    totalspace: 0,
  },
  checks: [],
  ops: [],
};

export default class FolderManageStore {

  template = initState.template;
  listParams = initState.listParams;

  @observable listViewStore = new ListViewPage.Store();
  @observable folderName = initState.folderName;
  @observable folderCounts = initState.folderCounts;
  @observable checks = initState.checks;
  @observable ops = initState.ops;

  @action
  getData = (params = { url: '', params: {} }, current) => {
    try {
      const { pageSize } = this.listViewStore;
      current = current || 1;
      Api.callApi(params).then(action((res) => {
        const { sessionkey } = res;
        Api.getDatas({
          dataKey: sessionkey,
          current,
          pageSize: pageSize,
        }).then(datares => {
          const { status, datas, ops } = datares;
          if (status) {
            this.ops = ops ? ops.filter(o => o.nodeName === 'operate') : [];
            const newState = {
              current,
              isTable: false,
              loading: false,
              data: [...(current === 1 ? [] : this.listViewStore.data), ...datas],
            };
            this.listViewStore.setState(newState);
            Api.getChecks({
              dataKey: sessionkey,
              randomDatas: JSON.stringify(datas),
            }).then(checkres => {
              if (checkres.status) {
                this.checks = [ ...(current === 1 ? [] : this.checks), ...checkres.datas];
                this.listViewStore.updateDataSource();
              }
            });
          }
        });
        Api.getCounts({
          dataKey: sessionkey,
        }).then(countres => {
          if (countres.status) {
            this.listViewStore.setState({ hasMore: countres.count > current * pageSize});
          }
        });
      }));
    } catch (e) {
      Bubble.info(e, 1, { top: '50%' });
    }
  }

  @action
  handleOperation = (params, cb) => {
    Api.operateUserMailFolder(params).then(res => {
      const { flag, message } = res;
      if (flag) {
        this.getData(this.listParams, 1);
        let remindInfo = getLabel('83885','操作成功！')
        switch (params.operation) {
          case "add":
            remindInfo = getLabel(83880, '添加成功！')
            break;
          case "del" || 'delmail' || "deletePermanently":
            remindInfo = getLabel(83472, '删除成功！')
            break;
          case "clearStar":
            remindInfo = getLabel(384426, '清空成功')
            break;
          default:
            remindInfo = getLabel('83885','操作成功！')
        }
        Bubble.success(remindInfo)
        if (cb) cb(res);
      } else {
        if(params.operation == "del"){
          if(res.msg){
            Bubble.error(res.msg, 1, { top: '50%' });
          }else{
            Bubble.error(getLabel(83473, '删除失败！'), 1, { top: '50%' });
          }
        }else{
          Bubble.error(message.replace(/repeat/, getLabel('519097','名称重复，操作失败')), 1, { top: '50%' });
        }
      }
    });
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)
}
